﻿$(document).ready(function () {

    $(".txtStartCreateDate,.txtEndCreateDate").val(DateFormat(new Date(), "yyyy/MM/dd"));
    var url = new Url();
    var $css = url.GetQueryString("css");

    $.ajax({
        type: "post",
        url: "../../../Handler/VerifyStatusHandler.ashx",
        dataType: "json",
        data: { type: 0 },
        async: false,
        cache: true,
        success: function (data) {
            var str = '<option value="">请选择</option><option value="-1">尚未立项</option>';
            $(data).each(function (i, e) {
                str += '<option value="' + e.VerifyId + '">' + e.VerifyName + '</option>';
            });
            $(".verifyStatus").html(str);
        }
    });

    //设置分页信息
    var page = new Page();
    page.Url = "../Handler/FinancialOrder.ashx";
    page.Dom = $("#searchArea");
    page.type = 22;
    page.CallBack = function (data) {
        $(".borrowList").empty();
        $.each(data, function (index, item) {
            item.Css = $css;
            if (!item.Right || item.Verify == 2 || item.Verify == 1 || item.Verify == 4 || item.Verify == 1021 || item.Verify == 1027) {
                $("#ContractTemplete").tmpl(item).appendTo($(".borrowList")).find(".edit,.delete").remove();
            }
            else if (item.Verify == 3) {
                $("#ContractTemplete").tmpl(item).appendTo($(".borrowList")).find(".delete").remove();
            }
            else {
                $("#ContractTemplete").tmpl(item).appendTo($(".borrowList"));
            }
        });
    };
    page.GetPageData();
    //搜索用户
    $(".btnSearchBorrow").bind("click", function () {
        var $userName = $(".txtName").val();
        var $phone = $(".txtPhone").val();
        $.getJSON("../Handler/FinancialUser.ashx",
            { UserName: $userName, Phone: $phone, type: 10, _: Math.random() },
            function (result) {
                $(".userInfo").empty();
                if (result.Status) {
                    $(result.Data).each(function (index, item) {
                        $("#UserTemplete").tmpl(item).appendTo($(".userInfo"));
                    });
                    $(".userInfo tr").css("cursor", "pointer").click(function () {
                        $(".txtUserName").val($(this).find(".name").text());
                        $(".txtUserId").val($(this).find(".userId").text());
                    });
                } else {
                    alert(result.Message);
                }
            });
    });
    $(".btnDelLender").bind("click", function () {
        $(".txtuserName").val("");
        $(".txtUserId").val("");
    });
    $(".btnShow").bind("click", function () {
        $(".txtName").val($(".txtUserName").val());
    })
    $(".txtuserName").bind("click", function () {
        $(".btnShow").click();
    });

});


//删除
function del(id) {
    if (confirm("确定要删除该条记录吗？")) {
        $.getJSON("../Handler/FinancialOrder.ashx", { Id: id, type: 3, _: Math.random() }, function (result) {
            if (result.Status) {
                $("#btnSearch").click();
            }
            else {
                alert(result.Message);
            }
        });
    }
    return false;
}

function DateFormat(date, fmt) { //author: meizz 
    var o = {
        "M+": date.getMonth() + 1, //月份 
        "d+": date.getDate(), //日 
        "h+": date.getHours(), //小时 
        "m+": date.getMinutes(), //分 
        "s+": date.getSeconds(), //秒 
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
        "S": date.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}